package org.qpcr.community.project.web.excel;

import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.Setter;
import org.hswebframework.reactor.excel.CellDataType;
import org.hswebframework.reactor.excel.ExcelHeader;
import org.hswebframework.web.authorization.Authentication;
import org.hswebframework.web.bean.FastBeanCopier;
import org.hswebframework.web.validator.ValidatorUtils;
import org.qpcr.community.project.model.entity.TestItemInstanceEntity;
import org.qpcr.core.metadata.Jsonable;

import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Getter
@Setter
public class TestItemExcelInfo implements Jsonable {

    @org.qpcr.community.io.excel.annotation.ExcelHeader(value = "检测项")
    @NotBlank(message = "检测项不能为空")
    private String item;

    @org.qpcr.community.io.excel.annotation.ExcelHeader(value = "检测项英文")
    @NotBlank(message = "检测项英文不能为空")
    private String itemEn;


    @org.qpcr.community.io.excel.annotation.ExcelHeader(value = "检测项编码")
    @NotBlank(message = "检测项编码不能为空")
    private String code;

    @org.qpcr.community.io.excel.annotation.ExcelHeader(value = "测光通道（VIC Cy5 FAM ROX）")
    @NotBlank(message = "测光通道不能为空")
    private String channel;

    private TestItemInstanceEntity testItemInstance;

    private Map<String, Object> configuration = new HashMap<>();

    private long rowNumber;

    private String state;

    public Map<String,Object> toMap(){
        return FastBeanCopier.copy(this,new HashMap<>());
    }

    public static List<ExcelHeader> getTemplateHeaderMapping(TestItemExcelFilterColumns filterColumns) {
        return Arrays.stream(new ExcelHeader[]{
                        new ExcelHeader("item", "检测项", CellDataType.STRING),
                        new ExcelHeader("itemEn", "检测项英文", CellDataType.STRING),
                        new ExcelHeader("code", "检测项编码", CellDataType.STRING),
                        new ExcelHeader("channel", "测光通道（VIC Cy5 FAM ROX）", CellDataType.STRING)
                })
                .filter(a -> !filterColumns.getColumns().contains(a.getKey()))
                .collect(Collectors.toList());
    }

    public static List<ExcelHeader> getExportHeaderMapping(TestItemExcelFilterColumns filterColumns) {
        return Arrays.stream(new ExcelHeader[]{
                        new ExcelHeader("item", "检测项", CellDataType.STRING),
                         new ExcelHeader("itemEn", "检测项英文", CellDataType.STRING),
                        new ExcelHeader("code", "检测项编码", CellDataType.STRING),
                        new ExcelHeader("channel", "测光通道名称 VIC Cy5 FAM ROX", CellDataType.STRING)
                })
                .filter(a -> !filterColumns.getColumns().contains(a.getKey()))
                .collect(Collectors.toList());
    }

    /**
     * 初始化保存实体
     *
     * @param testItemInstanceListDb 数据库已存在的数据
     * @param auth 认证数据
     * @return TestItemExcelInfo
     */
    public TestItemExcelInfo initTestItemInstance(List<TestItemInstanceEntity> testItemInstanceListDb, Authentication auth) {
        List<TestItemInstanceEntity> list = testItemInstanceListDb.stream()
                .filter(db -> db.getItem() != null && db.getItem().equals(this.item)
                        || db.getCode() != null && db.getCode().equals(this.getCode()))
                .collect(Collectors.toList());
        if (list.size() > 0) {
            throw new RuntimeException("检测项或检测项编号已存在");
        }
        TestItemInstanceEntity entity = FastBeanCopier.copy(this, new TestItemInstanceEntity());

        entity.setCreateTimeNow();
        entity.setCreatorId(auth.getUser().getId());
        entity.setCreatorName(auth.getUser().getName());

        entity.setModifyTimeNow();
        entity.setModifierId(auth.getUser().getId());
        entity.setModifierName(auth.getUser().getName());

        ValidatorUtils.tryValidate(entity);

        this.testItemInstance = entity;
        return this;
    }

    @Override
    public void fromJson(JSONObject json) {
        Jsonable.super.fromJson(json);
    }
}
